#!/usr/bin/python

import pygame
import game_engine.GameEngine

MOVE_NONE = 0
MOVE_LEFT = 1
MOVE_RIGHT = 2
MOVE_UP = 4
MOVE_DOWN = 8

# The input manager tracks user input and adjusts the main player entity as appropriate
class InputManager:

    # Obvious
    def __init__(self, playerEntity):
        # Init stuff here
        self.m_playerEntity=playerEntity
        self.m_lastMove=0
        self.m_tileSize = game_engine.GameEngine.getGameEngine().getLevelManager().getTileSize()
        self.m_PathFinder = game_engine.GameEngine.getGameEngine().getPathFinder()
        self.m_uiManager = game_engine.GameEngine.getGameEngine().getUIManager()
        self.m_timeBetweenMoves = 250


    # Obvious
    def cleanup(self):
        return None

    # Called once per frame to process user input
    def frameUpdate(self, elapsedms, absolutems):
        global MOVE_NONE
        global MOVE_LEFT
        global MOVE_RIGHT
        global MOVE_UP
        global MOVE_DOWN

        # Pump pygame input event queue
        pygame.event.pump()

        # Get the state of all pressed keys
        keyStates = pygame.key.get_pressed()
        
        # Get the state of all 

        # Check for movement
        movement = MOVE_NONE
        if keyStates[pygame.K_w] == True:
            movement += MOVE_UP
        if keyStates[pygame.K_s] == True:
            movement += MOVE_DOWN
        if keyStates[pygame.K_a] == True:
            movement += MOVE_LEFT
        if keyStates[pygame.K_d] == True:
            movement += MOVE_RIGHT
        if keyStates[pygame.K_f] == True:
            path = self.m_PathFinder.findPath(self.m_playerEntity,(45,2))
            self.m_playerEntity.tryFollowPath(path,250)
        if keyStates[pygame.K_g] == True:
            path = self.m_PathFinder.findPathToClosestReachable(self.m_playerEntity,(45,2))
            self.m_playerEntity.tryFollowPath(path,250)
        if keyStates[pygame.K_SPACE] == True:
            self.m_playerEntity.attack()
        else:
            self.m_playerEntity.stopAttack()

        if keyStates[pygame.K_1] == True:
            print 'Using Item'
            self.m_playerEntity.useInventoryItem(0)

        if keyStates[pygame.K_2] == True:
            print 'Using Item'
            self.m_playerEntity.useInventoryItem(1)

        if keyStates[pygame.K_3] == True:
            print 'Using Item'
            self.m_playerEntity.useInventoryItem(2)

        if keyStates[pygame.K_4] == True:
            print 'Using Item'
            self.m_playerEntity.useInventoryItem(3)
                
        # Handle left shift for "run" key
        if keyStates[pygame.K_LSHIFT] == True:
            self.m_timeBetweenMoves = 100
        else:
            self.m_timeBetweenMoves = 250

        if keyStates[pygame.K_t] == True:
            self.m_uiManager.addTextToConsole("Testing Again")



        # Check if any movement 
        self.handleMovement(movement, elapsedms, absolutems)


    # Handle movement by keys
    def handleMovement(self, movement, elapsedms, absolutems):
        global MOVE_NONE
        global MOVE_LEFT
        global MOVE_RIGHT
        global MOVE_UP
        global MOVE_DOWN

        # No movement, return
        if movement == MOVE_NONE:
            self.m_playerEntity.stand();
            return

        # Check for max movement rate
        if absolutems < (self.m_lastMove + self.m_timeBetweenMoves):
            return

        if movement == MOVE_UP:
            if self.m_playerEntity.tryMove(0,-1, self.m_timeBetweenMoves) != True:
                # TODO: Obnoxious cant-move-here sound?
                return
            self.m_lastMove = absolutems
        if movement == MOVE_DOWN:
            if self.m_playerEntity.tryMove(0,1, self.m_timeBetweenMoves) != True:
                # TODO: Obnoxious cant-move-here sound?
                return
            self.m_lastMove = absolutems
        if movement == MOVE_LEFT:
            if self.m_playerEntity.tryMove(-1,0, self.m_timeBetweenMoves) != True:
                # TODO: Obnoxious cant-move-here sound?
                return
            self.m_lastMove = absolutems
        if movement == MOVE_RIGHT:
            if self.m_playerEntity.tryMove(1,0, self.m_timeBetweenMoves) != True:
                # TODO: Obnoxious cant-move-here sound?
                return
            self.m_lastMove = absolutems


        # Otherwise multiple direction keys are pressed, ignore
        




